package com.dream.style.app.ac.home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.dream.style.R;

/**
 * @Created by lincyu
 * @Time: 2018/11/19 22:05
 * @Decs: 类
 */
public class GoodMagActivity extends AppCompatActivity {

    /** 上架 **/
    private TextView has;
    /** 仓库 **/
    private TextView whose;
    /** 滑线 **/
    private View line;
    private ViewGroup.MarginLayoutParams lp;
    private int w;
    private TextView curView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initEvent();
    }
    private void initEvent() {
        has.setSelected(true);
        curView = has;

        w = getResources().getDisplayMetrics().widthPixels / 2;
        lp = (ViewGroup.MarginLayoutParams) line.getLayoutParams();
        getWindow().getDecorView().post(new Runnable() {
            @Override
            public void run() {
                lp.width = (int) (has.getPaint().measureText(has.getText().toString()) + 0.5f);
                lp.leftMargin = (w - lp.width) / 2;
                line.setLayoutParams(lp);
            }
        });

        has.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!v.isSelected()){
                    v.setSelected(true);
                    curView.setSelected(false);
                    curView = (TextView) v;
                    // 画红线
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            toRight(false);
                        }
                    }).start();
                    //這裡切換數據。。。。。
                }
            }});

        whose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!v.isSelected()){
                    v.setSelected(true);
                    curView.setSelected(false);
                    curView = (TextView) v;
                    // 画红线
                    new Thread(new Runnable() {
                        @Override public void run() {
                            toRight(true);
                        }
                    }).start();
                    //這裡切換數據。。。。。
                }
            }
        });
    }

    private void toRight(final boolean b) {
        for (int i=0; i<w; i+=3){
            final int finalI = i;
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            getWindow().getDecorView().post(new Runnable() {
                @Override
                public void run() {
                    lp.width = (int) (curView.getPaint().measureText(
                            curView.getText().toString())+0.5f);
                    if (b){
                        lp.leftMargin = (w - lp.width) / 2 + finalI;
                    } else {
                        lp.leftMargin = (w - lp.width) / 2 + w - finalI;
                    }
                    line.setLayoutParams(lp);
                }

            });
        }
    }

    private void initView() {
        setContentView(R.layout.home_good_mag);
        has = findViewById(R.id.has);
        whose = findViewById(R.id.whose);
        line = findViewById(R.id.line);
    }

}
